﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;

namespace MoAds.AdAdapters.ZestAdz
{
    public class ZestAdzRequest
    {
        private Int32 numberOfAds = 5;

        public Int32 NumberOfAds 
        {
            get
            {
                return numberOfAds;
            }

            set
            {
                if (value > 5 || value < 1) throw new ArgumentOutOfRangeException("NumberOfAds");
                numberOfAds = value;
            }
        }

        public String ClientId { get; set; }
        public String MetaData { get; set; }
        public String Keywords { get; set; }
        public String UserAgent { get; set; }

        /// <summary>
        /// Gets the IP Address of phone
        /// </summary>
        /// <remarks>
        /// This is a fake IP address since there isn't a way to get the IP from WP7.
        /// </remarks>
        public String IPAddress
        {
            get
            {
                return "196.207.40.150";
            }
        }

        public String GetQueryString()
        {
            StringBuilder sb = new StringBuilder();

            AppendParameter(sb, "ua", UserAgent);
            AppendParameter(sb, "ip", IPAddress);
            AppendParameter(sb, "cid", ClientId);
            AppendParameter(sb, "meta", MetaData);
            AppendParameter(sb, "keyword", Keywords);
            AppendParameter(sb, "response_type", "xml");
            AppendParameter(sb, "recs", NumberOfAds.ToString());

            if (sb.Length > 1) sb.Remove(sb.Length - 1, 1);

            return sb.ToString();
        }

        private void AppendParameter(StringBuilder sb, String key, String value)
        {
            if (!String.IsNullOrEmpty(value))
            {
                sb.Append(key);
                sb.Append("=");
                sb.Append(Uri.EscapeDataString(value));
                sb.Append("&");
            }
        }
    }
}
